package com.waves.message.events.listener;

import com.waves.message.domain.entity.MessageRecord;
import com.waves.message.domain.enums.NoticeTypeEnums;
import com.waves.message.events.ISendMessageEvent;
import com.waves.message.strategy.AbstractINoticeStrategy;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * @author Eden
 * @date 2025/01/01 16:12
 **/
@Slf4j
@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SendMessageEventListener {

    @EventListener
    public void handlerInsert(ISendMessageEvent.insertEvent insertEvent) throws InstantiationException, IllegalAccessException {
        MessageRecord messageRecord = insertEvent.getMessageRecord();
        //枚举工厂+策略实现消息方法
        Optional<NoticeTypeEnums> enumByCode = NoticeTypeEnums.getEnumByCode(messageRecord.getNoticeType());
        boolean present = enumByCode.isPresent();
        if (present) {
            Class<? extends AbstractINoticeStrategy> noticeStrategy = enumByCode.get().getNoticeStrategy();
            if (noticeStrategy != null) {
                noticeStrategy.newInstance().sendMessage(messageRecord);
            }
        }
    }

}

